package com.taoyibao.hongcanting.ui.admin.delegate;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.ToolbarDelegate;
import com.taoyibao.hongcanting.widget.CountText;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description: 管理员的delegate
 * @date :${DATA} 16:02
 */
public class AdministratorDelegate extends ToolbarDelegate implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private FragmentManager mFragmentManager;
    private CountText mCountTextOne;
    private CountText mCountTextTwo;
    private CountText mCountTextThree;
    private ViewPager mViewPager;
    private FragmentStatePagerAdapter mAdapter;
    private List<Fragment> mFragments;
    private List<String> mTitleList;
    private List<CountText> mTabList;
    /**
     * 显示的pager的数量
     */
    private int mPagerCount;


    @Override
    public int getRootLayoutId() {
        return R.layout.activity_admin;
    }

    public void setFragmentManager(FragmentManager mFragmentManager, int pagerCount) {
        this.mFragmentManager = mFragmentManager;
        mPagerCount = pagerCount;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        mViewPager = get(R.id.tab_viewPager);
        mCountTextOne = get(R.id.countText_one);
        mCountTextOne.setOnClickListener(this);
        mCountTextTwo = get(R.id.countText_two);
        mCountTextTwo.setOnClickListener(this);
        mCountTextThree = get(R.id.countText_three);
        mCountTextThree.setOnClickListener(this);
        mFragments = new ArrayList<>();
        mTitleList = new ArrayList<>();
        mTabList = new ArrayList<>();
        if (mPagerCount == 2) {
            mTabList.add(mCountTextOne);
            mTabList.add(mCountTextTwo);
            mCountTextThree.setVisibility(View.GONE);
        } else if (mPagerCount == 3) {
            mTabList.add(mCountTextOne);
            mTabList.add(mCountTextTwo);
            mTabList.add(mCountTextThree);
        }
        mViewPager.addOnPageChangeListener(this);
    }

    /**
     * 设置Fragment集合
     *
     * @param frgmentList
     * @param titleList
     */
    public void addViewPageTab(List<Fragment> frgmentList, List<String> titleList) {
        mFragments.addAll(frgmentList);
        mTitleList.addAll(titleList);
        initTitle();
        setAdapteData();
    }

    /**
     * 初始化tab上的文字
     */
    private void initTitle() {
        for (int i = 0; i < mTabList.size(); i++) {
            mTabList.get(i).setViewText(mTitleList.get(i));
        }
    }


    /**
     * 设置适配器
     */
    public void setAdapteData() {
        mAdapter = new FragmentStatePagerAdapter(mFragmentManager) {
            @Override
            public int getCount() {
                return mTitleList == null ? 0 : mTitleList.size();
            }

            @Override
            public Fragment getItem(int index)//直接创建fragment对象并返回
            {
                return mFragments.get(index);
            }

            @Override
            public CharSequence getPageTitle(int position) {

                return mTitleList == null ? super.getPageTitle(position) : mTitleList.get(position);
            }
        };
        mViewPager.setAdapter(mAdapter);
        setCurrentPager(0);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        setCurrentPager(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.countText_one:
                setCurrentPager(0);
                break;
            case R.id.countText_two:
                setCurrentPager(1);
                break;
            case R.id.countText_three:
                setCurrentPager(2);
                break;
        }
    }

    /**
     * 设置当前显示的页面
     *
     * @param position
     */
    public void setCurrentPager(int position) {
        mViewPager.setCurrentItem(position);
        for (int i = 0; i < mTabList.size(); i++) {
            if (i == position) {
                mTabList.get(i).setSelected(true);
            } else {
                mTabList.get(i).setSelected(false);
            }
        }
    }

    /**
     * 设置tab上的数量
     *
     * @param pager 当前的pager
     * @param count 要显示的数量
     */
    public void setTabCount(int pager, int count) {
        if (mTabList != null && mTabList.size() > pager) {
            mTabList.get(pager).setCount(count + "", true);
        }
    }

    /**
     * 设置一次加载多少个页面
     *
     * @param totlaCount
     */
    public void setOffscreenPageLimit(int totlaCount) {
        mViewPager.setOffscreenPageLimit(totlaCount);
    }
}
